<?
include("session.php");
$role_name = $_SESSION['role_name'];
$id = "";
if(isset($_GET['id'])) {
	$id = $_GET['id'];
}
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="../webkit/utils/jquery.js"></script>
<script type="text/javascript" src="../webkit/utils/form.checker.js"></script>
<script type="text/javascript" src="../webkit/datepicker/WdatePicker.js"></script>
<link type="text/css" rel="stylesheet" href="page.css"/>
<link type="text/css" rel="stylesheet" href="../webkit/aw/aw.css"/>   
<body  bgcolor="#f4f4f4" style="margin:0px;" scroll=no>
<style>
	.tab {
		font-size:12px; background:#ccc;  border:gray 1px solid; cursor:pointer; margin-right:6px; padding:2px 5px;
	}
	.tab_current {
		background:#b0d2fd;  border-bottom-color:#6600ff;
	}
</style>

<!-- Tab Begin -->
<span class='tab' id='order_info' onclick='show_tab(this.id)'>工单信息</span>
<?if($_GET['op'] == "edit") {?>
<span class='tab' id='handle_info' onclick='show_tab(this.id)'>处理信息</span> 
<span class='tab' id='history_info' onclick='show_tab(this.id)'>操作历史</span>
<?}?>
<!-- Tab End -->
<hr size=1>

<table width=100% cellspacing=1 cellpadding=3 id="order_info_panel"> 
	<form name="f1">
	<tr>
		<td align=left colspan=4 bgcolor=#cccccc><b>客户信息</b></td>
	</tr>
	<tr>
        <td align=right nowrap>客户编号</td><td><input type='text' id="client_no" class='input' check='not_empty' warning='客户编号不能为空！' onkeydown="if(event.keyCode==13){get_client_by_code(this.value)}"><img src='../webkit/portal/images/query.png' style='cursor:hand' title='输入客户编号进行查询' onclick='get_client_by_code(f1.client_no.value)'></td>  
		<td align=right>服务到期</td><td><input type="text" value="" id="exp_date" name="exp_date" class="datepicker" onClick="WdatePicker({readOnly:true, skin:'whyGreen',dateFmt:'yyyy-MM-dd'})" size="10"></td>
	</tr>
	<tr>
		<td align=right>客户名称</td><td><input type='text' id="client_name" class='input' check='not_empty' warning='客户名称不能为空！'></td>
		<td align=right>联系手机</td><td><input type='text' id="mobilephone" class='input' check='not_empty' warning='联系手机不能为空！' onkeydown="if(event.keyCode==13){get_client_by_phone(this.value)}"><img src='../webkit/portal/images/query.png' style='cursor:hand' title='输入客户手机号码进行查询' onclick='get_client_by_phone(f1.mobilephone.value)'></td>
	</tr>
	<tr>
		<td align=right>联系地址</td><td colspan=3><input type='text' id="address" class='input' check='not_empty' warning='联系地址不能为空！' style="width:500px"></td>
	</tr>
	<tr>
		<td align=right nowrap>客户类型</td><td><select id="client_type" class='input' check='integer' warning='请选择客户类型！'><option value=''>-请选择-</option><option value=1>单次</option><option value=2>包年</option><option value=3>VIP</option></select></td>
		<td align=right>联系电话1</td><td><input type="text" id="telephone1" class='input'></td>
	</tr>
	<tr>
		<td align=right nowrap>联系电话2</td><td><input type='text' id="telephone2" class='input'></td>
		<td align=right nowrap>联系电话3</td><td><input type='text' id="telephone3" class='input'></td>
	</tr>
	</form>
	<tr>
		<td align=left colspan=4 bgcolor=#cccccc><b>工单信息</b></td>
	</tr>
	<form name="f2">
	<tr>
		<td align=right>受理座席</td><td><input type='text' id="agent_name" class='input' check='not_empty' warning='受理座席不能为空！' readonly value="<?echo $_SESSION['uid']?>"></td>
		<td align=right>受理时间</td><td><input type='text' id="call_time" class='input' check='not_empty' warning='受理时间不能为空！' value="<?echo date("Y-m-d H:i:s")?>"></td>
	</tr>
	<tr>
		<td align=right>工单编号</td><td><input type='text' id="order_no" class='input' readonly></td>
		<td align=right>来电号码</td><td><input type='text' id="call_number" class='input' value="<?echo $_SESSION['call_number']?>"></td>
	</tr>
	<tr>
		<td align=right>指派处理</td><td><select id="user_name" class='input'><option>-请选择-</option></select></td>
		<td align=right>预约时间</td><td><input type='text' id="appt_time" class="datepicker" onClick="WdatePicker({readOnly:false, skin:'whyGreen',dateFmt:'yyyy-MM-dd HH:mm:ss'})" value="<?echo date("Y-m-d H:i:s", strtotime("+5 Hours"))?>"  check='not_empty' warning='预约时间不能为空！'></td>
	</tr>
	<tr>
		<td align=right>故障描述</td><td colspan=3><textarea id="problem_desc" style="width:500px" rows=3 check='not_empty' warning='故障描述不能为空！'></textarea></td>
	</tr>
	<tr>
		<td align=center colspan=4>
		<hr size=1>
		<input type="button" id="btn_save_client" class="button" value="保存客户信息" onclick="save_client()">　　<input type="button" id="btn_save_order" class="button" value="保存工单信息" onclick="save_order()">　　<input type="button" id="btn_dispatch_order" class="button" value="派单" onclick="dispatch_order()">　　<input type="button" id="btn_change_appt" class="button" value="改约" onclick="change_appt()">　　<input type="button" id="btn_hasten_order" class="button" value="催单" onclick="hasten_order()">　　<input type="button" class="button" value="关闭窗口" onclick="parent.closeDlg()" always_enabled="yes"></td>
	</tr>
	<input type='hidden' id="id">
	<input type='hidden' id="client_id" value='<?echo $_GET['client_id']?>'>
	</form>
</table>


<table width=100% cellspacing=1 cellpadding=3 id="handle_info_panel" style="display:none">
	<form name="f3">
	<tr>
		<td align=left colspan=4 bgcolor=#cccccc><b>收单信息</b></td>
	</tr>
	<tr>
		<td align=right nowrap>回单时间</td><td><input type='text' id="accept_time" class="input" readonly style="color:blue"></td>
		<td align=right nowrap>工单状态</td><td>
			<select id="order_status">
			<option value=1>待处理</option>
			<option value=2>已派单</option>
			<option value=4>已改约</option>
			<option value=3>回单</option>
			</select>
		</td>
	</tr>
	<tr>
		<td align=right>回单时限</td><td><input type='text' id="expire_time"  class="input" readonly style="color:red"></td>
		<td align=right></td><td></td>
	</tr>
	<tr>
		<td align=right valign=top>收单备注</td><td colspan=3><textarea id="accept_text" style="width:500px" rows=2 user_flag="worker"></textarea><br>
		<input type="button" value="确认回单" class="button" onclick="accept_order()" user_flag="worker" id="btn_accept_order">　　<input type="button" class="button" value="关闭窗口" onclick="parent.closeDlg()" user_flag="worker" always_enabled="yes"></td>
	</tr>
	<tr>
		<td align=left colspan=4 bgcolor=#cccccc><b>回访信息</b></td>
	</tr>
	<tr>
		<td align=right nowrap>回访工号</td><td><input type='text' id="callback_user" class='input' readonly value="<?echo $_SESSION['uid']?>"></td>
		<td align=right nowrap>回访结果</td><td><select id="callback_result"><option value=1>非常满意</option><option value=2>满意</option><option value=3>一般</option><option value=4>不满意</option></select></td>
	</tr>
	<tr>
		<td align=right valign=top>回访备注</td><td colspan=3><textarea id="callback_text" style="width:500px" rows=2  user_flag="callback"></textarea><br>
		<input type="button" value="确认回访" id="btn_callback_order" class="button" onclick="callback_order()" user_flag="callback">　　<input type="button" class="button" value="关闭窗口" onclick="parent.closeDlg()" user_flag="callback" always_enabled="yes"></td>
	</tr>
	<tr>
		<td align=left colspan=4 bgcolor=#cccccc><b>质检信息</b></td>
	</tr>
	<tr>
		<td align=right nowrap>确认工号</td><td><input type='text' id="check_user" class='input' readonly value="<?echo $_SESSION['uid']?>"></td>
		<td align=right nowrap>确认结果</td><td><select id="check_result"><option value=1>工单完成</option><option value=2>已处理</option><option value=3>重派单</option></select></td>
	</tr>
	<tr>
		<td align=right valign=top>确认备注</td><td colspan=3><textarea id="check_text" style="width:500px" rows=2></textarea><br>
		<input type="button" value="确认质检" id="btn_check_order" class="button" onclick="check_order()" user_flag="check">　　<input type="button" class="button" value="关闭窗口" onclick="parent.closeDlg()" user_flag="check" always_enabled="yes"></td>
	</tr>
	</div>
	</form>
</table>

<table width=100% cellspacing=1 cellpadding=3 id="history_info_panel" style="display:none">
	<tr>
		<td height=350 style="border-bottom:gray 1px solid; overflow:auto; line-height:20px" id="history_text" valign=top>&nbsp;</td>
	</tr>
	<tr>
		<td align=center><input type="button" class="button" value="关闭窗口" onclick="parent.closeDlg()"></td>
	</tr>
</table>

<script type="text/javascript">
	
	var f1 = document.f1;
	var f2 = document.f2;
	var f3 = document.f3;

	function save_client() { 
		if(FormChecker.doCheck(f1)) {
			var params = {};
			var els = f1.elements;
			params['id'] = f2.client_id.value
			for(var i=0; i<els.length; i++) {
				if(els[i].type == "button") continue;
				params[els[i].id] = els[i].value;
			}
			var op = "edit";
			if(params['id'] == '') op = "add";
			$('#btn_save_client').attr('disabled', true);
			$.post("client_query.php?action="+op, params, function(data) {
				if(data.indexOf("ok") == 0) {
					alert("数据保存成功!");
					if(op == 'add') { // 临时新增客户后返回id值，存于form2中
						f2.client_id.value = data.split(",")[1]
					}
				} else {  
					alert(data);
				}
				$('#btn_save_client').attr('disabled', false);
			});
		}
	}; 


	function save_order() { 
		if(f2.client_id.value == '') {
			alert("对不起，当前客户信息为空！请先保存或查询客户信息！");
			return false;
		}
		if(FormChecker.doCheck(f2)) {
			var params = {};
			var els = f2.elements;
			for(var i=0; i<els.length; i++) {
				if(els[i].type == "button") continue;
				params[els[i].id] = els[i].value;
			}
			var op = "<?echo $_GET['op']?>";
			$('#btn_save_order').attr('disabled', true);
			$.post("order_query.php?action="+op, params, function(data) {
				if(data.indexOf("ok") == 0) {
					alert("数据保存成功!");
					parent.refreshGrid();
					parent.closeDlg();
				} else {  
					alert(data);
				}
				$('#btn_save_order').attr('disabled', false);
			});
		}
	}; 


	function dispatch_order() { 
		if(f2.order_no.value == '') {
			alert("对不起，当前工单记录为空！请先保存工单信息！");
			return false;
		}
		if(f2.user_name.value == '') {
			alert("请选择指派处理人！");
			f2.user_name.focus();
			return false;
		}
		if(confirm('确认要派单给[ '+f2.user_name.value+' ]吗？')) {
			var params = {'order_no':f2.order_no.value, 'user_name':f2.user_name.value}
			$.post("order_query.php?action=dispatch_order", params, function(data) {
				if(data.indexOf("ok") == 0) {
					alert("派单操作成功!");
					parent.refreshGrid();
					parent.closeDlg();
				} else {  
					alert(data);
				}
			});
		}
	}

	function accept_order() { 
		if(f3.accept_text.value == '') {
			alert("请输入备注内容！");
			return false;
		}
		if(confirm('确认要进行此操作吗？')) {
			var params = {'order_no':f2.order_no.value, 'accept_text':f3.accept_text.value, 
			 'order_status':3}
			$.post("order_query.php?action=accept_order", params, function(data) {
				if(data.indexOf("ok") == 0) {
					alert("回单操作成功!");
					parent.refreshGrid();
					parent.closeDlg();
				} else {  
					alert(data);
				}
			});
		}
	}

	function hasten_order() { 
		if(confirm('确认要进行催单吗？')) {
			var params = {'order_no':f2.order_no.value}
			$.post("order_query.php?action=hasten_order", params, function(data) {
				if(data.indexOf("ok") == 0) {
					alert("催单操作成功!");
					parent.refreshGrid();
					parent.closeDlg();
				} else {  
					alert(data);
				}
			});
		}
	}

	function change_appt() { 
		if(confirm('确认要进行改约吗？')) {
			var params = {'order_no':f2.order_no.value, 'appt_time':f2.appt_time.value}
			$.post("order_query.php?action=change_appt", params, function(data) {
				if(data.indexOf("ok") == 0) {
					alert("改约操作成功!");
					parent.refreshGrid();
					parent.closeDlg();
				} else {  
					alert(data);
				}
			});
		}
	}

	function callback_order() {
		if(f3.callback_text.value == '') {
			alert("请输入备注内容！");
			return false;
		}
		if(confirm('确认要进行此操作吗？')) {
			var params = {'order_no':f2.order_no.value, 'callback_result':f3.callback_result.value, 
				'callback_text':f3.callback_text.value}
			$.post("order_query.php?action=callback_order", params, function(data) {
				if(data.indexOf("ok") == 0) {
					alert("回访确认成功!");
					parent.refreshGrid();
					parent.closeDlg();
				} else {  
					alert(data);
				}
			});
		}
	}

	function check_order() {
		if(f3.check_text.value == '') {
			alert("请输入备注内容！");
			return false;
		}
		if(confirm('确认要进行此操作吗？')) {
			var params = {'order_no':f2.order_no.value, 'check_result':f3.check_result.value, 
				'check_text':f3.check_text.value}
			$.post("order_query.php?action=check_order", params, function(data) {
				if(data.indexOf("ok") == 0) {
					alert("质检确认成功!");
					parent.refreshGrid();
					parent.closeDlg();
				} else {  
					alert(data);
				}
			});
		}
	}

	var order_status= 0; // 工单状态
	var user_flag = '<?echo $_SESSION["user_flag"]?>'; // 用户权限标识

	function init() {
		<?
		if($_GET['op'] == 'edit') {
		?>
		$.get("order_query.php?action=get&id=<?echo $_GET['id']?>", function(data){
			eval("var row = "+data);
			for(var k in row) {
				$('#'+k).val(row[k]);
			}
			order_status = row['order_status'];
			init_history(row['order_no']); // 初始化工单操作历史
			init_client(row['client_id']); // 修改工单时，要自动读取客户信息
			init_user_permission(); // 初始化用户界面操作权限
		})
		<?} else {?>
			if(f2.client_id.value != '') {
				init_client(f2.client_id.value); // 如果新增时指定了客户，则初始化
			}
		<?}?>

	}

	function init_user_permission() {
		
		if(user_flag != 'admin') { 

		// 非管理员角色的界面权限处理----BEGIN
			disable_form_elements(f1)
			disable_form_elements(f2);
			disable_form_elements(f3)
			show_tab('handle_info');
			enable_user_elements();   
		// 非管理员角色的界面权限处理----END	

		} else {

			if(order_status==3 && (login_uid=='jx01' || login_uid=='jx02')) {
				 // 已经回单后，两个座席(admin)用户不允许再修改
				disable_form_elements(f1);
				disable_form_elements(f2);
				$("#btn_accept_order").attr('disabled', true);
				$("#accept_text").attr("readonly", true);;
			}
		}
		
	}


	function disable_form_elements(f) {
		var els = f.elements;
		for(var i=0; i<els.length; i++) {
			els[i].readOnly = true; // 文本框只读 
			if(els[i].tagName == "SELECT" || els[i].type == "button") {
				if($(els[i]).attr("always_enabled") != 'yes') {
					$(els[i]).attr('disabled', true); // 按钮或下拉框无效 (除开指定了always_enabled)
				}
			}
		}
	}

	var login_uid = '<?=$_SESSION["uid"]?>';
	function enable_user_elements() {
		if(order_status==3 && user_flag=="worker") {
			return false; // 已经回单后，外勤不允许再修改
		}
		$("#handle_info_panel input").each(
			function() {
				var obj = $(this);
				if(obj.attr("user_flag") == user_flag) {
					obj.attr('disabled', false);
				}
			}	
		);
		// 这个方法，如果采用上面那种form遍历的形式居然不行...不解！
		if(user_flag == "worker") {
			$("#accept_text").attr("readonly", false);
		} else if(user_flag == "callback") {
			$("#callback_text").attr("readonly", false);
			$("#callback_result").attr("disabled", false);
		} else if(user_flag == "check") {
			$("#check_text").attr("readonly", false);
			$("#check_result").attr("disabled", false);
		}
	}

	function init_client(id) {
		$.get("client_query.php?action=get&id="+id, function(data){
			eval("var row = "+data);
			for(var k in row) {
				if(k == 'id') continue; // 过滤掉client中的id，以免和order中的冲突
				$("#"+k).val(row[k]);
			}
		})
	}

	
	function init_history(order_no) {
		$.get("order_query.php?action=get_history&order_no="+order_no, function(data){
			eval("var list = "+data);
			var html = []
			for(var i=0; i<list.length; i++) {
				html.push("<li> [<font color=blue>"+ list[i]['op_user'] +"</font>] 进行操作：<font color=red>"+ list[i]['op_desc'] +"</font>，<font color=gray>@"+ list[i]['op_time'] +"</font>");
			}
			$("#history_text").html(html.join(""));
		});
	}


	function get_client_by_phone(n) {
		if(n == '') {
			alert("请输入客户手机号码进行查询！");
			return false;
		}
		if(confirm('确认要查询号码[ '+n+' ]对应的客户？')) {
			$.get("client_query.php?action=get_client_by_phone&number="+n, function(data){
				if(data == "no record") {
					alert("对不起，没有找到相应记录！");
					return false;
				}
				eval("var row = "+data);
				for(var k in row) {
					if(k == 'id') continue; // 过滤掉client中的id，以免和order中的冲突
					$("#"+k).val(row[k]);
				}
				f2.client_id.value = row['id']; // 将id直接赋给order中的字段
			});
		}
	}


	function get_client_by_code(n) {
		if(n == '') {
			alert("请输入客户编号进行查询！");
			return false;
		}
		if(confirm('确认要查询编号[ '+n+' ]对应的客户？')) {
			$.get("client_query.php?action=get_client_by_code&code="+n, function(data){
				if(data == "no record") {
					alert("对不起，没有找到相应记录！");
					return false;
				}
				eval("var row = "+data);
				for(var k in row) {
					if(k == 'id') continue; // 过滤掉client中的id，以免和order中的冲突
					$("#"+k).val(row[k]);
				}
				f2.client_id.value = row['id']; // 将id直接赋给order中的字段
			});
		}
	}

	$(function() {
		$.get("user_query.php?action=get_select_options", function(data) {
			var arr = data.split("$");
			for(var i=0; i<arr.length; i++){
				var kv = arr[i].split(',');
				f2.user_name.options[i+1] = new Option(kv[1], kv[0]);
			}
		});
		init();
	});


	$('#order_info').addClass('tab_current');
	var current_tab_id = 'order_info';
	function show_tab(id) {
		if(id != current_tab_id) {
			$('#'+current_tab_id).removeClass('tab_current');
			$('#'+current_tab_id+'_panel').hide();
			$('#'+id).addClass('tab_current');
			$('#'+id+'_panel').show();
			current_tab_id = id;
		}
	}
</script>
</body>
</html>